package com.example.springboot.utilabstract;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author 冯渊
 * @Date 2018-12-26 12:04:00
 * @Description 日期单元
 */
public abstract class BaseDate {

    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private static final String[] WEEK = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};

    /**
     * 校验格式
     */
    private static boolean chkPattern(String pattern) {
        if (StringUtils.isBlank(pattern)) {
            return false;
        }
        return !(!"yyyyMMddHHmmssSSS".equals(pattern) &&
                !"yyMMddHHmmssSSS".equals(pattern) &&
                !"yyyyMMddHHmmss".equals(pattern) &&
                !"yyyyMMdd".equals(pattern) &&
                !"HHmmss".equals(pattern) &&
                !"yyyy-MM-dd HH:mm:ss".equals(pattern) &&
                !"yyyy-MM-dd HH:mm:ss:SSS".equals(pattern) &&
                !"yyyy-MM-dd".equals(pattern) &&
                !"HH:mm:ss".equals(pattern) &&
                !"MM/dd/yyyy".equals(pattern));
    }

    /**
     * 格式化时间
     */
    public static String dateToStr(Date date, String pattern) {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 格式化时间
     */
    public static Date dateToDate(Date date, String pattern) {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateStr = sdf.format(date);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * timestampToStr
     */
    public static String timestampToStr(Timestamp time, String pattern) {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        cal.setTime(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(cal.getTime());
    }

    /**
     * timestampToDate
     */
    public static Date timestampToDate(Timestamp time, String pattern) throws ParseException {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        cal.setTime(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateStr = sdf.format(cal.getTime());
        return sdf.parse(dateStr);
    }

    /**
     * 将时间转换为时间戳
     */
    public static Long dateToStamp(String s, String pattern) throws ParseException {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = simpleDateFormat.parse(s);
        return date.getTime();
    }

    /*
    * 将时间戳转换为时间
    */
    public static Date stampToDate(Long stamp, String pattern) throws ParseException {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        return dateToDate(new Date(stamp), pattern);
    }

    /**
     * strToDate 转换时间字符串 2015-05-29 12:12:45 返回Date
     */
    public static Date strToDate(String timeStr, String pattern) {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * strToTimestamp
     */
    public static Timestamp strToTimestamp(String timeStr, String pattern) throws ParseException {
        Date date = strToDate(timeStr, pattern);
        return new Timestamp(date.getTime());
    }

    /**
     * 根据日期 yyyy-MM-dd 计算当前星期几
     */
    public static String dayForWeek(String pTime) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(pTime));
        int dayForWeek = 7;
        if (c.get(Calendar.DAY_OF_WEEK) != 1) {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return WEEK[dayForWeek - 1];
    }

    /**
     * 获取日期 pattern null或"" 返回 yyyy-MM-dd HH:mm:ss
     */
    public static String getDate(Date date, String pattern) {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        return sf.format(date);
    }

    /**
     * 日期加减
     */
    public static Date dateAddDays(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date); // 设置当前日期
        c.add(Calendar.DATE, day); // 日期分钟加1,Calendar.DATE(天),Calendar.HOUR(小时)
        date = c.getTime(); // 结果
        return date;
    }

    /**
     * 日期加减
     */
    public static String dateAddDays(Date date, String pattern, Integer day) {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        date = dateAddDays(date, day);
        return sf.format(date);
    }

    /**
     * 转换时间字符串20150529121245转换0000-00-00 00:00:00 返回String
     */
    public static String strToDate(String time) {
        String n, y, d, s, f, m;
        n = time.substring(0, 4);
        y = time.substring(4, 6);
        d = time.substring(6, 8);
        s = time.substring(8, 10);
        f = time.substring(10, 12);
        m = time.substring(12, 14);
        return n + "-" + y + "-" + d + " " + s + ":" + f + ":" + m;
    }

    /**
     * 比较两个日期的大小 releDate大于nowDate返回true 反之false
     */
    public static boolean dateCompare(String releDate, String nowDate, String pattern) throws ParseException {
        if (!chkPattern(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        DateFormat df = new SimpleDateFormat(pattern);
        Date d1 = df.parse(releDate);
        Date d2 = df.parse(nowDate);
        return d1.getTime() > d2.getTime();
    }

    /**
     * 计算两个字符串的日期格式的之间相差的天数
     */
    public static int daysBetween(String smdate, String bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 计算两个字符串的日期格式的之间相差的毫秒数
     */
    public static int daysBetweenMsec(String smdate, String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(smdate));
            long time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            long time2 = cal.getTimeInMillis();
            long between_msec = time2 - time1;
            return Integer.parseInt(String.valueOf(between_msec));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 计算两个字符串的日期格式的之间相差的天数
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return daysBetween(sdf.format(smdate), sdf.format(bdate));
    }

    /**
     * 计算两个字符串的日期格式的之间相差的毫秒数
     */
    public static int daysBetweenMsec(Date smdate, Date bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        return daysBetweenMsec(sdf.format(smdate), sdf.format(bdate));
    }

    /**
     * 获取当前时间
     */
    public static Date getCurrDate() {

        Date date = new Date();
        try {
            return dateToDate(date, "yyyy-MM-dd HH:mm:ss:SSS");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取当前时间
     */
    public static String getCurrDate(String pattern) {

        return dateToStr(new Date(), pattern);
    }

    public static void main(String[] args) {
        System.out.println(BaseDate.getCurrDate("yyMMddHHmmssSSS"));
    }
}
